//
//  NSString+ClearCache.m
//  MovieDate
//
//  Created by ann on 16/3/22.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "NSString+ClearCache.h"

@implementation NSString (ClearCache)
-(NSInteger)fileSize{
    
 NSFileManager *mgr =   [NSFileManager defaultManager];
    BOOL dir = NO;
    BOOL exists =  [mgr fileExistsAtPath:self isDirectory:&dir];
    //文件\文件夹不存在
    if (exists == NO) {
        return 0;
    }
    if (dir) {
        NSArray *subpaths = [mgr subpathsAtPath:self];
        NSInteger totalByteSize = 0;
        for (NSString *subpath in  subpaths) {
            NSString *fullsubpath = [self stringByAppendingPathComponent:subpath];
            //判断是否为文件
            BOOL dir = NO;
            [mgr fileExistsAtPath:fullsubpath isDirectory:&dir];
            if (dir == NO) {
                totalByteSize += [[mgr attributesOfItemAtPath:fullsubpath error:nil][NSFileSize] integerValue];
            }
        }
        return totalByteSize;
    }else{
    //self 是一个文件
        return [[mgr attributesOfItemAtPath:self error:nil] [NSFileSize] integerValue];
    }
    

}
@end
